package util.content;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import replaceWords.Complex;
import replaceWords.MarkDealXML;

public class ContentRex {

	List sourceSiteList = null;

	public String getTitle() {
		for (Iterator iterSourceSite = sourceSiteList.iterator(); iterSourceSite.hasNext();) {
			Element sourceSite = (Element) iterSourceSite.next();
			String titleRex = sourceSite.getChildText("titleRex");
			Pattern p = Pattern.compile(titleRex);
			Matcher m = p.matcher(title);
			if (m.find()) {
				title = m.group(1).trim();
			}
		}
		return title;
	}

	String contentString = "";

	public String getContentString() {

		Complex wordToWords = new Complex();
		MarkDealXML mdx = new MarkDealXML();
		mdx.setMarkList(markList);
		mdx.setMarkSuccedaneumsList(markSuccedaneumsList);
		String partialContent = null;
		ContentCapture cc = new ContentCapture();
		for (Iterator iterSourceSite = sourceSiteList.iterator(); iterSourceSite.hasNext();) {
			Element sourceSite = (Element) iterSourceSite.next();
			List spiderList = sourceSite.getChildren("spider");
			for (Iterator iterSpider = spiderList.iterator(); iterSpider.hasNext();) {
				Element spider = (Element) iterSpider.next();
				// 抓取文章内容
				List contentList = spider.getChildren("content");
				for (Iterator iterContent = contentList.iterator(); iterContent.hasNext();) {
					Element content = (Element) iterContent.next();
					// 抓取文章内容
					List contentRexList = content.getChildren("contentget");
					for (Iterator itercontentRexList = contentRexList.iterator(); itercontentRexList.hasNext();) {
						Element contentRexE = (Element) itercontentRexList.next();
						String contentRex = contentRexE.getText();
						// System.out.println(contentRex);
						// 调用ContentCapture对象
						cc.setContentRex(contentRex);
						partialContent = cc.getContent(bodyString);
						//System.out.println(partialContent);
					}
					List contentdelList = content.getChildren("contentdel");
					for (Iterator iterContentdel = contentdelList.iterator(); iterContentdel.hasNext();) {
						Element contentdel = (Element) iterContentdel.next();
						// 拿到需要删除的内容的正则
						String delRex = contentdel.getText();
						cc.setDelRex(delRex);
						//System.out.println(partialContent.replaceAll("<.*display:none.*>[\\w\\W]*?</.*>", "").replaceAll("<p.*>\\s", "<p>").replaceAll("<pre[\\w\\W]*?>\\s", "<pre>").replaceAll("<span[\\w\\W]*?>\\s", "<span>").trim());
						partialContent = cc.del(partialContent).replaceAll("<p.*>", "<p>").replaceAll("<pre[\\w\\W]*?>", "<pre>").replaceAll("<span[\\w\\W]*?>", "<span>").trim();						
						//System.out.println(partialContent);
					}
					// 这里拼了一个格式
					contentString = contentString + partialContent;
					//System.out.println(contentString);
					// 过滤每行开始和结束后的空格
					ArrayList<String> linelineString = new ArrayList<String>();
					Pattern onelinePattern = Pattern.compile(".+");
					Matcher onelineMatcher = onelinePattern.matcher(contentString);
					while (onelineMatcher.find()) {
						linelineString.add(onelineMatcher.group().trim());
					}

					contentString = "";
					for (int i = 0; i < linelineString.size(); i++) {

						contentString = contentString + linelineString.get(i) + "\n";
					}
					//System.out.println(contentString);
					// 识别中英文行
					for (int lineId = 0; lineId < linelineString.size(); lineId++) {
						String oneLineTxt = linelineString.get(lineId).trim();
						if (oneLineTxt.startsWith("<") && oneLineTxt.endsWith(">")) {
							continue;
						} else {
							ChineseWordsCounter cwc = new ChineseWordsCounter();
							Pattern chineseWordPattern = Pattern.compile("[\u4e00-\u9fa5]");
							Matcher chineseWordmatcher = chineseWordPattern.matcher(oneLineTxt);
							if (chineseWordmatcher.find() && cwc.count(oneLineTxt)) {
								oneLineTxt = mdx.markdel(oneLineTxt);
								try {
									oneLineTxt = wordToWords.segWords(oneLineTxt, " ");
									oneLineTxt = mdx.markAdd(oneLineTxt);
									linelineString.set(lineId, oneLineTxt);
								} catch (IOException e) {

									e.printStackTrace();
								}

							} else {
								// System.out.println(oneLineTxt);
								Pattern pBags = Pattern.compile("[a-zA-Z]{2,100}");
								Matcher mBags = pBags.matcher(oneLineTxt);
								while (mBags.find()) {
									String s = mBags.group();
									oneLineTxt = oneLineTxt.replaceAll(s, "<zf" + s + "zf>");
								}
								oneLineTxt = oneLineTxt.replaceAll("\\<[zf<]*zf", "<zf").replaceAll("[zf>]*\\>", "zf>");
								linelineString.set(lineId, oneLineTxt.trim());
							}
						}
					}
					// contentString清除原来的内容装入去掉空格的内容
					contentStringToTranslate = "";
					for (int i = 0; i < linelineString.size(); i++) {
						contentStringToTranslate = contentStringToTranslate + linelineString.get(i) + "\n";
					}
				}
			}
		}
		//System.out.println(contentString);
		return contentString;
	}

	ContentRex() {
		SAXBuilder builder = new SAXBuilder(false);
		try {
			String xmlpath = "spiderParameter.xml";
			Document doc = builder.build(xmlpath);
			Element sourceSite = doc.getRootElement();
			sourceSiteList = sourceSite.getChildren("sourceSite");
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			Document doc = builder.build("D:\\MyTool\\markList.xml");
			Element wordsElement = doc.getRootElement();
			List rowslist = wordsElement.getChildren("row");
			for (Iterator iter = rowslist.iterator(); iter.hasNext();) {
				Element row = (Element) iter.next();
				List fieldList = row.getChildren("field");
				for (Iterator iterField = fieldList.iterator(); iterField.hasNext();) {
					Element field = (Element) iterField.next();
					String fieldName = field.getAttributeValue("name");
					if (fieldName.equalsIgnoreCase("mark")) {
						String word = field.getText();
						markList.add(word);
					} else {
						String newone = field.getText();
						markSuccedaneumsList.add(newone);
					}
				}
			}
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	String bodyString = null;

	public void setBodyString(String bodyString) {
		this.bodyString = bodyString;
	}

	String title = null;

	public void setTitle(String title) {
		this.title = title;
	}

	String contentStringToTranslate = null;

	public String getContentStringToTranslate() {
		return contentStringToTranslate;
	}

	List<String> markList = new ArrayList<String>();
	List<String> markSuccedaneumsList = new ArrayList<String>();
}
